﻿function PollutantLineChart(config) {
    this.TimePoints = [];
    this.SO2 = [];
    this.NO2 = [];
    this.CO = [];
    this.O3 = [];
    this.O3_8H = [];
    this.PM25 = [];
    this.PM10 = [];
    this.AQI = [];
    this.AQIType = [];
    this.AQILevel = [];
    this.PrimaryPollutant = [];
    this.AQIAvg = [];
    this.AQIAvgType = [];
    this.timePointFormat = config["TimePointFormat"] || null;
    this.height = config["Height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["Xwidth"] || null;
    this.subTitle = config["SubTitle"] || null;
    this.name = config["Name"] || "";
    this.container = config['Container'] || null;
    this.jsonData = config["JsonData"];    //数据源
    this.series = [];    //曲线


}

PollutantLineChart.prototype.initData = function () {
    var pollutantChart = this;
    var data = this.jsonData;
    var dataLength = data.length;

    if (dataLength > 0) {
        for (var i = 0; i < dataLength; i++) {

            //曲线图公共内容
            var timePoint = GetTimeFromJsonString(data[i].TimePoint).format(pollutantChart.timePointFormat);
            pollutantChart.TimePoints.push(timePoint);
            pollutantChart.SO2.push(data[i].SO2);
            pollutantChart.NO2.push(data[i].NO2);
            pollutantChart.CO.push(data[i].CO);
            pollutantChart.O3.push(data[i].O3);
            pollutantChart.PM25.push(data[i].PM2_5);
            pollutantChart.PM10.push(data[i].PM10);
            pollutantChart.AQI.push(data[i].AQI);
            pollutantChart.AQILevel.push(data[i].AQILevel);
            pollutantChart.PrimaryPollutant.push(data[i].PrimaryPollutant);
            pollutantChart.AQIType.push(AirIndex(data[i].AQI).Type);


            //曲线图不同内容
            if ("undefined" != typeof (data[i].O3_8H)) {
                pollutantChart.O3_8H.push(data[i].O3_8H);
            }
            if ("undefined" != typeof (data[i].AQIAverage)) {
                pollutantChart.AQIAvg.push(data[i].AQIAverage);
                pollutantChart.AQIAvgType.push(AirIndex(data[i].AQIAverage).Type);
            }

        }
        pollutantChart.getSeries();
        pollutantChart.createChart();
    }
    else {
        pollutantChart.createNoDataChart();
    }
};

PollutantLineChart.prototype.getSeries = function () {
    var pollutantChart = this;


    if (pollutantChart.AQIAvg.length > 0) {
        pollutantChart.series.push({
            name: '全市AQI值',
            type: 'spline',
            data: pollutantChart.AQIAvg
        });
    }

    pollutantChart.series.push({
        name: pollutantChart.name + 'AQI值',
        type: 'spline',
        data: pollutantChart.AQI
    });
};

PollutantLineChart.prototype.createChart = function () {
    var pollutantChart = this;
    this.chart = new Highcharts.Chart(this.getChartOptions(), function (chart) {
        pollutantChart.onChartLoad(chart);
    });
};

PollutantLineChart.prototype.createNoDataChart = function () {
    var pollutantChart = this;

    var nodataOptions = {
        chart: {
            renderTo: pollutantChart.container,
            height: pollutantChart.height,
            width: pollutantChart.width
        },
        title: {
            text: '暂无数据'
        },
        series: [{
            type: 'line',
            name: pollutantChart.name+ 'AQI值',
            data: []
        }],
        lang: {
            noData: "Nichts zu anzeigen"
        },
        noData: {
            style: {
                fontWeight: 'bold',
                fontSize: '15px',
                color: '#303030'
            }
        }
    };
    this.chart = new Highcharts.Chart(nodataOptions, function (chart) {
        pollutantChart.onChartLoad(chart);
    });
};


PollutantLineChart.prototype.getChartOptions = function () {
    var pollutantChart = this;
    return {
        chart: {
            renderTo: pollutantChart.container,
            height: pollutantChart.height,
            width: pollutantChart.width
        },
        credits: {
            enabled: false
        },
        title: {
            text: pollutantChart.subTitle + pollutantChart.name + 'AQI趋势图'
        },
        xAxis: [{
            categories: pollutantChart.TimePoints,
            tickInterval: pollutantChart.getStep()
        }],
        yAxis: {
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            },
            plotLines: levelStandardLine
        },
        tooltip: {
            crosshairs: true,
            useHTML: true,
            shared: true,
            formatter: function () {
                return pollutantChart.tooltipFormatter(this);
            }
        },
        series: pollutantChart.series
    };
};


PollutantLineChart.prototype.getStep = function () {
    var pollutantChart = this,
        width = $("#" + pollutantChart.container).width(),
        count = pollutantChart.TimePoints.length,
        num = 1,
        xWidth = pollutantChart.xwidth;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

PollutantLineChart.prototype.tooltipFormatter = function (tooltip) {
    var index = tooltip.points[0].point.x;
    var pointObject = tooltip.points;


    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table class="table_tooltip">';

    for (var i = 0, count = pointObject.length; i < count; i++) {
        var point = pointObject[i].point;
        var series = pointObject[i].series;

        var aqiValue = Highcharts.pick(point.value, point.y);
        var aqiType = AirIndex(aqiValue).Type;
        
        var aqiTr = '<tr><td colspan="5"><span style="color:' + series.color + '">\u25CF</span><strong>' + series.name + ': ' + aqiValue + "   " + aqiType + '</strong></td></tr>';

        ret += aqiTr;
    }



   
    ret += '<tr><td colspan="5">首要污染物: ' + pollutantNameSubTransform(this.PrimaryPollutant[index]) + '</td></tr>' +
        '<tr><td  width="55px" align="right">SO<sub>2</sub>:</td><td  width="35px" class="color-blue"> ' + this.SO2[index] + '</td><td width="10px">&nbsp;</td><td width="55px" align="right">NO<sub>2</sub>:</td><td class="color-blue" width="35px"> ' + this.NO2[index] + '</td></tr>'+
        '<tr><td align="right">CO:</td><td class="color-blue"> ' + this.CO[index] + '</td><td >&nbsp;</td><td align="right">O<sub>3</sub>: </td><td class="color-blue">' + this.O3[index] + '</td></tr>' +
          '<tr><td align="right">PM<sub>10</sub>:</td><td class="color-blue"> ' + this.PM10[index] + '</td><td>&nbsp;</td><td align="right">PM<sub>2.5</sub>:</td><td class="color-blue"> ' + this.PM25[index] + '</td></tr>';


    if (this.O3_8H.length != 0) {
        ret += '<tr><td align="right">O<sub>3</sub>_8H:</td><td class="color-blue"> ' + this.O3_8H[index] + '</td></tr>';
    }

    ret += '</table>';
    return ret;
};

PollutantLineChart.prototype.onChartLoad = function (chart) {
    this.removeLogo();
};

PollutantLineChart.prototype.removeLogo = function () {
    var chartText = $("text[text-anchor='end']");
    chartText.each(function () {
        if ($(this).text() == 'Highcharts.com') {
            $(this).remove();
        }
    });
};


